package com.gitee.wsl.common.ui.ext.shape

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import com.gitee.wsl.common.ui.ext.path.slice
import com.gitee.wsl.unit.angle.AngleUnit


/**
 * Creates a pie chart slice shape with a total angular extent of [angle] degrees with an
 * optional holeSize that is specified as a percentage of the overall slice radius.
 * The pie diameter is equal to the Shape's size width. The slice is positioned with its vertex
 * at the center.
 */
class Slice(
    private val startAngle: AngleUnit,
    private val angle: AngleUnit,
    private val innerRadius: Float = 0.5f,
    private val outerRadius: Float = 1.0f,
) : Shape {
    override fun createOutline(
        size: Size,
        layoutDirection: LayoutDirection,
        density: Density,
    ): Outline {
        val widthHalf = size.width / 2f
        val radius = widthHalf * outerRadius
        val holeRadius = widthHalf * innerRadius
        val center = Offset(widthHalf, widthHalf)

        return Outline.Generic(
            Path().apply {
                slice(
                    startAngle = startAngle,
                    angle = angle,
                    center = center,
                    radius = radius,
                    holeRadius = holeRadius,
                    widthHalf = widthHalf
                )
            }
        )
    }
}

fun Shapes.slice(
    startAngle: AngleUnit,
    angle: AngleUnit,
    innerRadius: Float = 0.5f,
    outerRadius: Float = 1.0f,
) = Slice(startAngle, angle, innerRadius, outerRadius)


